package com.gbr.client;

import javafx.embed.swing.JFXPanel;

import com.gbr.application.action.ShutdownAction;
import com.ng.framework.AbstractDSComponent;
import com.ng.framework.service.action.IActionListener;
import com.ng.framework.service.action.IActionService;
import com.ng.framework.service.log.ILogService;
import com.ng.framework.service.log.LogLevel;

public class JavaFxRunner extends AbstractDSComponent implements IJavaFxReady {
    private ILogService    logService    = null;
    private IActionService actionService = null;
    private JFXPanel       context       = null;
    private ShutdownActionListener shutdownListener = null;

    @Override
    protected void activateComponent() {
        logService.log(getClass(), LogLevel.TRACE, "Activating");

        // this launches the javaFx toolkit
        // can't find a better way to do this.
        context = new JFXPanel();
        
        shutdownListener = new ShutdownActionListener();
        actionService.addListener(ShutdownAction.class, shutdownListener);
    }

    @Override
    protected void deactivateComponent() {
        logService.log(getClass(), LogLevel.TRACE, "Deactivating");
        actionService.removeListener(ShutdownAction.class, shutdownListener);
    }

    public final void bindService(ILogService service) {
        logService = service;
    }

    public final void unbindService(ILogService service) {
        logService = null;
    }

    public final void bindService(IActionService service) {
        actionService = service;
    }

    public final void unbindService(IActionService service) {
        actionService = null;
    }

    class ShutdownActionListener implements IActionListener<ShutdownAction> {
        @Override
        public void actionPerformed(ShutdownAction action) {
            System.out.println("*** Runner received shutdown...");
            context = null;
        }
    }
}
